//
//  TrackingController.swift
//  ExpressDriver
//
//  Created by sheng on 2017/11/10.
//  Copyright © 2017年 sheng. All rights reserved.
//

import UIKit
import MapKit
import SnapKit

class TrackingController: UIViewController,UITableViewDataSource,UITableViewDelegate,RatingBarDelegate {
    
    
    var scrollView: UIScrollView!
    
    var ordernoInput: UITextField!
    
    var companyBtn: UIButton!
    
    var line1: UIView!
    
    var bingLabel: UILabel!
    
    var addressLabel: UILabel!
    
    var orderNoLabel: UILabel!
    
    var line2: UIView!
    
    var trackLabel: UILabel!
    
    var trackMap: MKMapView!
    
    var locationLabel: UILabel!
    
    var loacationMap: MKMapView!
    
    var driverLabel: UILabel!
    
    var driverImage: UIImageView!
    
    var driverInfo: UILabel!
    
    var phoneBtn: UIButton!
    
    var whatAppBtn: UIButton!
    
    var messageBtn: UIButton!
    
    var ratingBar : WNRatingBar?
    
    var alertController: UIAlertController!
    
    var companyList:[String] = ["ebay.com","netflix.com","anazon.co.uk","walmart.com","target.com",
                                "bestbuy.com","ikea.com","stores.ebay.com","newegg.com","homedepot.com",
                                "overstock.com","sky.com","macys.com","tickectmaster.com","autos.yahoo.com",
                                "gap.com","seras.com,Searcs","zaoppos.com","barensandnoble.com"]
    
    let statusHeight = UIApplication.shared.statusBarFrame.height
    var scrrenHeight = UIScreen.main.bounds.height
    var scrrenWidth = UIScreen.main.bounds.width

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        setUI();
    }
    
    
    func setUI() {
        scrollView = UIScrollView()
        scrollView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
        scrollView.contentSize.width = scrrenWidth
        scrollView.isScrollEnabled = true
        scrollView.showsVerticalScrollIndicator = true
        scrollView.indicatorStyle = .black
        scrollView.alwaysBounceVertical = true
        scrollView.alwaysBounceHorizontal = false
        self.view.addSubview(scrollView)
        
        
        let imgLock1 =  UIImageView(frame:CGRect(x: 7.5, y: 7.5, width: 15, height: 15))
        imgLock1.image = UIImage(named:"search")
        ordernoInput = UITextField()
        ordernoInput.placeholder = "search"
        ordernoInput.font = UIFont.systemFont(ofSize: 12)
        ordernoInput.leftView = UIView(frame:CGRect(x: 0, y: 0, width: 30, height: 30))
        ordernoInput.leftViewMode = UITextFieldViewMode.always
        ordernoInput.leftView!.addSubview(imgLock1)
        ordernoInput?.layer.borderColor = UIColor.gray.cgColor
        ordernoInput?.layer.borderWidth = 0.5
        ordernoInput?.layer.cornerRadius = 5
        scrollView.addSubview(ordernoInput)
        ordernoInput.snp.makeConstraints { (make) in
            make.top.equalTo(scrollView.snp.top).offset(15)
            make.left.equalTo(scrollView.snp.left).offset(10)
            make.height.equalTo(25)
            make.width.equalTo(150)
        }
        
        companyBtn = UIButton()
        companyBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        companyBtn.setTitle("amazon.com", for: UIControlState())
        companyBtn.setTitleColor(UIColor.black, for: UIControlState())
        companyBtn.layer.borderColor = UIColor.lightGray.cgColor
        companyBtn.layer.borderWidth = 0.5
        companyBtn.layer.cornerRadius = 4
        companyBtn.addTarget(self, action: #selector(selectCompany), for: .touchUpInside)
        scrollView.addSubview(companyBtn)
        companyBtn.snp.makeConstraints { (make) in
            make.top.equalTo(scrollView.snp.top).offset(15)
            make.width.equalTo(150)
            make.right.equalTo(-10)
        }
        
        
        ratingBar = WNRatingBar()
        ratingBar?.setSeletedState("star_big1", halfSelectedName: "star_big2", fullSelectedName: "star_big3", starSideLength: 20, delegate: self )
        ratingBar?.displayRating(8)
        scrollView.addSubview(ratingBar!)
        ratingBar?.snp.makeConstraints({ (make) in
            make.top.equalTo(ordernoInput.snp.bottom).offset(20)
            make.width.equalTo(100)
            make.height.equalTo(12)
            make.centerX.equalTo(scrollView.snp.centerX)
        })
        
        
        line1 =  UIView()
        line1.backgroundColor = UIColor.lightGray
        scrollView.addSubview(line1)
        line1.snp.makeConstraints { (make) in
            make.top.equalTo((ratingBar?.snp.bottom)!).offset(10)
            make.height.equalTo(0.5)
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.centerX.equalTo(self.view)
        }
        
    
        bingLabel = UILabel()
        bingLabel.text = "Billing Information"
        bingLabel.textColor = UIColor.black
        bingLabel.font = UIFont.systemFont(ofSize: 16)
        scrollView.addSubview(bingLabel)
        bingLabel.snp.makeConstraints { (make) in
            make.top.equalTo(line1.snp.bottom).offset(10)
            make.left.equalTo(10)
        }
        
        
        addressLabel = UILabel()
        addressLabel.numberOfLines = 0
        addressLabel.text = "Billing address1、2、3, price"
        addressLabel.font = UIFont.systemFont(ofSize: 14)
        scrollView.addSubview(addressLabel)
        addressLabel.snp.makeConstraints { (make) in
            make.top.equalTo(bingLabel.snp.bottom).offset(5)
            make.left.equalTo(bingLabel.snp.left).offset(15)
            make.right.equalTo(-15)
        }
        
        orderNoLabel = UILabel()
        orderNoLabel.numberOfLines = 0
        orderNoLabel.text = "Order No, third party number"
        orderNoLabel.font = UIFont.systemFont(ofSize: 14)
        scrollView.addSubview(orderNoLabel)
        orderNoLabel.snp.makeConstraints { (make) in
            make.top.equalTo(addressLabel.snp.bottom).offset(3)
            make.left.equalTo(addressLabel.snp.left)
            make.right.equalTo(-15)
        }

        line2 =  UIView()
        line2.backgroundColor = UIColor.lightGray
        scrollView.addSubview(line2)
        line2.snp.makeConstraints { (make) in
            make.top.equalTo(orderNoLabel.snp.bottom).offset(10)
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.height.equalTo(0.5)
            make.centerX.equalTo(self.view)
        }
        
        trackLabel = UILabel()
        trackLabel.text = "Tracking Detail"
        trackLabel.font = UIFont.systemFont(ofSize: 16)
        scrollView.addSubview(trackLabel)
        trackLabel.snp.makeConstraints { (make) in
            make.top.equalTo(line2.snp.bottom).offset(10)
            make.left.equalTo(10)
        }
        
        trackMap = MKMapView()
        trackMap.frame = CGRect(x: 0, y: 0, width: scrrenWidth, height: 250)
        trackMap.mapType = .standard
        trackMap.isScrollEnabled = true
        trackMap.isZoomEnabled = true
        scrollView.addSubview(trackMap)
        trackMap.snp.makeConstraints { (make) in
            make.top.equalTo(trackLabel.snp.bottom).offset(10)
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.size.equalTo(CGSize(width: scrrenWidth, height: 250))
        }
        
        locationLabel = UILabel()
        locationLabel.text = "Location Detail"
        locationLabel.font = UIFont.systemFont(ofSize: 16)
        scrollView.addSubview(locationLabel)
        locationLabel.snp.makeConstraints { (make) in
            make.top.equalTo(trackMap.snp.bottom).offset(10)
            make.left.equalTo(10)
        }
        
        loacationMap = MKMapView()
        loacationMap.frame = CGRect(x: 0, y: 0, width: scrrenWidth, height: 250)
        loacationMap.mapType = .standard
        loacationMap.isScrollEnabled = true
        loacationMap.isZoomEnabled = true
        scrollView.addSubview(loacationMap)
        loacationMap.snp.makeConstraints { (make) in
            make.top.equalTo(self.locationLabel.snp.bottom).offset(10)
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.size.equalTo(CGSize(width: scrrenWidth, height: 250))
        }
        
        
        driverLabel = UILabel()
        driverLabel.text = "Driver Information"
        driverLabel.font = UIFont.systemFont(ofSize: 16)
        scrollView.addSubview(driverLabel)
        driverLabel.snp.makeConstraints { (make) in
            make.top.equalTo(loacationMap.snp.bottom).offset(10)
            make.left.equalTo(10)
        }
        
        
        driverImage = UIImageView(image:UIImage(named:"ic_launcher_round"))
        driverImage?.layer.borderColor = UIColor.gray.cgColor
        driverImage?.layer.borderWidth = 1.0
        scrollView.addSubview(driverImage)
        driverImage.snp.makeConstraints { (make) in
            make.top.equalTo(driverLabel.snp.bottom).offset(10)
            make.centerX.equalTo(scrollView.snp.centerX)
            make.height.equalTo(100)
            make.width.equalTo(100)
        }
        
        driverInfo = UILabel()
        driverInfo.numberOfLines = 0
        driverInfo.text = "Driver name, Company information, Company name, short description"
        driverInfo.font = UIFont.systemFont(ofSize: 14)
        scrollView.addSubview(driverInfo)
        driverInfo.snp.makeConstraints { (make) in
            make.top.equalTo(driverImage.snp.bottom).offset(10)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.centerX.equalTo(scrollView.snp.centerX)
        }
        
        phoneBtn = UIButton()
        phoneBtn.setImage(UIImage(named: "phone"), for: UIControlState.normal)
        scrollView.addSubview(phoneBtn)
        phoneBtn.snp.makeConstraints { (make) in
            make.top.equalTo(driverInfo.snp.bottom).offset(15)
            make.left.equalTo((scrrenWidth - 135)/2)
            make.height.equalTo(35)
            make.width.equalTo(35)
        }
        
        
        whatAppBtn = UIButton()
        whatAppBtn.setImage(UIImage(named: "whatsapp"), for: UIControlState.normal)
        scrollView.addSubview(whatAppBtn)
        whatAppBtn.snp.makeConstraints { (make) in
            make.top.equalTo(phoneBtn.snp.top)
            make.left.equalTo(phoneBtn.snp.right).offset(15)
            make.height.equalTo(35)
            make.width.equalTo(35)
        }
        
        messageBtn = UIButton()
        messageBtn.setImage(UIImage(named: "message"), for: UIControlState.normal)
        scrollView.addSubview(messageBtn)
        messageBtn.snp.makeConstraints { (make) in
            make.top.equalTo(whatAppBtn.snp.top)
            make.left.equalTo(whatAppBtn.snp.right).offset(15)
            make.height.equalTo(35)
            make.width.equalTo(35)
        }
        
       
    }
    
    
    

    func selectCompany(_ sender: UIButton) {
        alertController = UIAlertController(title: "\n\n\n\n\n\n\n\n\n\n", message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
        
        let companyTableView: UITableView = UITableView()
        
        companyTableView.dataSource = self
        companyTableView.delegate = self
        let nib = UINib(nibName: String(describing: CompanyTypeCell.self), bundle: nil)
        companyTableView.register(nib, forCellReuseIdentifier: "companyName")
        companyTableView.estimatedRowHeight = 44.0
        companyTableView.rowHeight = UITableViewAutomaticDimension
        companyTableView.frame = CGRect(x: 10, y: 10, width: alertController.view.bounds.size.width - 40, height: 190)
        alertController.addAction(UIAlertAction(title: "cancel", style: UIAlertActionStyle.cancel,handler:nil))
        
        alertController.view.addSubview(companyTableView)
        
        self.present(alertController, animated: true, completion: nil)
    }
    
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return companyList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "companyName", for: indexPath) as! CompanyTypeCell
        cell.selectionStyle = .none
        cell.companyNameLabel.text = companyList[indexPath.row]
        return cell
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        print("第\(indexPath.row)行被点击了")
        self.companyBtn.titleLabel?.text = companyList[indexPath.row]
        DispatchQueue.main.async {
            self.alertController?.dismiss(animated: false, completion: nil)
        }
    }
    
    
    
    
    override func viewDidAppear(_ animated: Bool) {
        
        scrollView.contentSize.width = scrrenWidth
        scrollView.contentSize.height = scrrenHeight + 350
       
    }
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func ratingChanged(_ ratingBar: WNRatingBar, newRating: Float) {
        
    }
    

}
